class FlowNode {
    constructor(type, id, position) {
        this.type = type;
        this.id = id;
        this.position = position;
        this.params = {
            input: [],
            output: []
        };
        this.name = '';
        this.prompt = '';
        this.modelType = 'gpt-3.5-turbo';
        this.temperature = 0.7;

        // 不同节点类型的特定属性
        switch (type) {
            case 'code':
                this.code = '# 请输入Python代码\n\ndef process(data):\n    # 处理数据\n    result = data\n    return result';
                this.language = 'python';
                break;
            case 'ifelse':
                this.condition = '';
                break;
            case 'iterator':
                this.iterationKey = 'items';
                this.maxIterations = 10;
                break;
            case 'template':
                this.template = '{{ result }}';
                break;
            case 'http':
                this.url = '';
                this.method = 'GET';
                this.headers = {};
                this.requestBody = {};
                break;
            case 'database':
                this.connectionString = '';
                this.query = '';
                break;
            case 'agent':
                this.agentPrompt = '';
                this.tools = [];
                break;
            case 'email':
                this.to = '';
                this.cc = '';
                this.bcc = '';
                this.subject = '';
                this.body = '';
                this.attachments = [];
                break;
        }
    }

    createDOMElement() {
        const node = document.createElement('div');
        node.id = this.id;
        node.className = `flow-node ${this.type}-node`;
        node.style.left = `${this.position.x}px`;
        node.style.top = `${this.position.y}px`;

        const header = document.createElement('div');
        header.className = 'node-header';

        const icon = document.createElement('i');
        switch (this.type) {
            case 'start':
                icon.className = 'fas fa-play-circle';
                break;
            case 'model':
                icon.className = 'fas fa-brain';
                break;
            case 'end':
                icon.className = 'fas fa-stop-circle';
                break;
            case 'agent':
                icon.className = 'fas fa-robot';
                break;
            case 'ifelse':
                icon.className = 'fas fa-code-branch';
                break;
            case 'iterator':
                icon.className = 'fas fa-redo';
                break;
            case 'code':
                icon.className = 'fas fa-code';
                break;
            case 'template':
                icon.className = 'fas fa-file-alt';
                break;
            case 'http':
                icon.className = 'fas fa-globe';
                break;
            case 'database':
                icon.className = 'fas fa-database';
                break;
            case 'email':
                icon.className = 'fas fa-envelope';
                break;
        }

        const title = document.createElement('span');
        title.className = 'node-title';
        title.textContent = this.name || this.getDefaultName();

        header.appendChild(icon);
        header.appendChild(title);
        node.appendChild(header);

        const content = document.createElement('div');
        content.className = 'node-content';

        // 根据节点类型添加不同的内容
        switch (this.type) {
            case 'model':
                const modelInfo = document.createElement('div');
                modelInfo.className = 'model-info';

                const modelType = document.createElement('div');
                modelType.className = 'model-type';
                modelType.innerHTML = `<i class="fas fa-cog"></i> <span>${this.modelType}</span>`;

                const modelPrompt = document.createElement('div');
                modelPrompt.className = 'model-prompt';
                modelPrompt.textContent = this.prompt ? (this.prompt.length > 50 ? this.prompt.substring(0, 50) + '...' : this.prompt) : '未设置提示词';

                modelInfo.appendChild(modelType);
                modelInfo.appendChild(modelPrompt);
                content.appendChild(modelInfo);
                break;

            case 'agent':
                const agentInfo = document.createElement('div');
                agentInfo.className = 'agent-info';
                agentInfo.innerHTML = `<i class="fas fa-tools"></i> <span>可用工具: ${this.tools.length}</span>`;
                content.appendChild(agentInfo);
                break;

            case 'code':
                const codeInfo = document.createElement('div');
                codeInfo.className = 'code-info';
                codeInfo.innerHTML = `<i class="fas fa-code"></i> <span>${this.language}</span>`;
                content.appendChild(codeInfo);
                break;

            case 'ifelse':
                const conditionInfo = document.createElement('div');
                conditionInfo.className = 'condition-info';
                conditionInfo.innerHTML = `<i class="fas fa-question-circle"></i> <span>${this.condition || '未设置条件'}</span>`;
                content.appendChild(conditionInfo);
                break;

            case 'iterator':
                const iteratorInfo = document.createElement('div');
                iteratorInfo.className = 'iterator-info';
                iteratorInfo.innerHTML = `<i class="fas fa-list"></i> <span>迭代键: ${this.iterationKey}</span>`;
                content.appendChild(iteratorInfo);
                break;

            case 'template':
                const templateInfo = document.createElement('div');
                templateInfo.className = 'template-info';
                templateInfo.innerHTML = `<i class="fas fa-file-alt"></i> <span>模板转换</span>`;
                content.appendChild(templateInfo);
                break;

            case 'http':
                const httpInfo = document.createElement('div');
                httpInfo.className = 'http-info';
                httpInfo.innerHTML = `<i class="fas fa-globe"></i> <span>${this.method} ${this.url || '未设置URL'}</span>`;
                content.appendChild(httpInfo);
                break;

            case 'database':
                const dbInfo = document.createElement('div');
                dbInfo.className = 'db-info';
                dbInfo.innerHTML = `<i class="fas fa-database"></i> <span>数据库查询</span>`;
                content.appendChild(dbInfo);
                break;

            case 'email':
                const emailInfo = document.createElement('div');
                emailInfo.className = 'email-info';

                const emailRecipient = document.createElement('div');
                emailRecipient.className = 'email-recipient';
                emailRecipient.innerHTML = `<i class="fas fa-user"></i> <span>收件人: ${this.to || '未设置'}</span>`;

                const emailSubject = document.createElement('div');
                emailSubject.className = 'email-subject';
                emailSubject.innerHTML = `<i class="fas fa-heading"></i> <span>主题: ${this.subject || '未设置'}</span>`;

                emailInfo.appendChild(emailRecipient);
                emailInfo.appendChild(emailSubject);
                content.appendChild(emailInfo);
                break;
        }

        if (this.params.input.length > 0 || this.params.output.length > 0) {
            const paramsPreview = document.createElement('div');
            paramsPreview.className = 'params-preview';

            if (this.params.input.length > 0) {
                const inputPreview = document.createElement('div');
                inputPreview.className = 'input-preview';
                inputPreview.innerHTML = `<i class="fas fa-arrow-right"></i> 输入: ${this.params.input.length}项`;
                paramsPreview.appendChild(inputPreview);
            }

            if (this.params.output.length > 0) {
                const outputPreview = document.createElement('div');
                outputPreview.className = 'output-preview';
                outputPreview.innerHTML = `<i class="fas fa-arrow-left"></i> 输出: ${this.params.output.length}项`;
                paramsPreview.appendChild(outputPreview);
            }

            content.appendChild(paramsPreview);
        }

        node.appendChild(content);

        return node;
    }

    getDefaultName() {
        switch (this.type) {
            case 'start':
                return '开始节点';
            case 'model':
                return '大模型节点';
            case 'end':
                return '结果节点';
            case 'agent':
                return '智能体节点';
            case 'ifelse':
                return '条件判断节点';
            case 'iterator':
                return '迭代器节点';
            case 'code':
                return '代码节点';
            case 'template':
                return '模板转换节点';
            case 'http':
                return 'HTTP节点';
            case 'database':
                return '数据库节点';
            case 'email':
                return '邮件节点';
            default:
                return '未命名节点';
        }
    }

    updateParams(params) {
        this.params = params;
    }

    updateName(name) {
        this.name = name;
    }

    updatePrompt(prompt) {
        this.prompt = prompt;
    }

    toJSON() {
        const json = {
            id: this.id,
            type: this.type,
            position: this.position,
            params: this.params,
            name: this.name
        };

        // 根据节点类型添加特定属性
        switch (this.type) {
            case 'model':
                json.prompt = this.prompt;
                json.modelType = this.modelType;
                json.temperature = this.temperature;
                break;
            case 'code':
                json.code = this.code;
                json.language = this.language;
                break;
            case 'ifelse':
                json.condition = this.condition;
                break;
            case 'iterator':
                json.iterationKey = this.iterationKey;
                json.maxIterations = this.maxIterations;
                break;
            case 'template':
                json.template = this.template;
                break;
            case 'http':
                json.url = this.url;
                json.method = this.method;
                json.headers = this.headers;
                json.requestBody = this.requestBody;
                break;
            case 'database':
                json.connectionString = this.connectionString;
                json.query = this.query;
                break;
            case 'agent':
                json.agentPrompt = this.agentPrompt;
                json.tools = this.tools;
                break;
            case 'email':
                json.to = this.to;
                json.cc = this.cc;
                json.bcc = this.bcc;
                json.subject = this.subject;
                json.body = this.body;
                json.attachments = this.attachments;
                break;
        }

        return json;
    }
}

// 节点工厂
class NodeFactory {
    static createNode(type, position) {
        const id = `node_${Date.now()}`;
        return new FlowNode(type, id, position);
    }
}